/**********************************************************************************
Copyright (C), by AppleCai
Project                      : Study Kernel driver
Description                  : User Application for cdev
History                      :
CPU and Compiler             : AM335x,GCC
GenDate                      : 2020-11-8
OwnerBy                      : Apple Cai
**********************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#include "stdlib.h"
#include "string.h"

#define CHIP                "/dev/i2c-2"
#define CHIP_ADDR           0x51
int main(int argc,char **argv) 
{
         int fd =open(CHIP, O_RDWR);
         if (fd< 0) {
                   printf("open"CHIP"failed\n");
                   goto exit;
         }

 

         if (ioctl(fd,I2C_SLAVE_FORCE, CHIP_ADDR) < 0) {
                   printf("oictl:setslave address failed\n");
                   goto close;
         }

 


         unsigned char  rddata=0x0;
         unsigned char  wrdata=0x0;
         unsigned char  dataaddr=0x0;     
         int ret =0;
         struct i2c_rdwr_ioctl_data work_queue;                          
         if(strcmp(argv[1], "r") == 0)
         {
         printf("writeaddress return: %d\n",write(fd, &dataaddr, 1));      
         printf("readdata return:%d\n", read(fd, &rddata, 1));
         printf("rddata:0x%x\n", rddata);
         }
         else if(strcmp(argv[1], "w") == 0)
         {
          wrdata = atoi(argv[2]);
          #if 0
          printf("writeaddress return: %d\n",write(fd, &dataaddr, 1));      
          printf("writedata return:%d\n", write(fd, &wrdata, 1));
          printf("wrdata:0x%x\n", wrdata);   
         #endif

          work_queue.nmsgs = 1;
          work_queue.msgs = (struct i2c_msg*)malloc(work_queue.nmsgs * sizeof(struct i2c_msg));
          if(!work_queue.msgs)
          {
            printf("Memory alloc error\n");
            goto close;
          }
          ioctl(fd,I2C_TIMEOUT,2);
          ioctl(fd,I2C_RETRIES,1);
          int size = 1;

          (work_queue.msgs[0]).len = size+1;      /* len = 1 byte data and 1 byte addr */
          (work_queue.msgs[0]).addr = CHIP_ADDR;  /* device addr */
          //(work_queue.msgs[0]).flags = I2C_M_WR;  /* write flag */
          (work_queue.msgs[0]).buf = &dataaddr;   /* data addr */
          //((work_queue.msgs[0]).buf+1) = &wrdata; /* data value */
          memcpy(((work_queue.msgs[0]).buf + 1), &wrdata, size);   /* data value */

          ret = ioctl(fd,I2C_RDWR,(unsigned long)&work_queue);
          if(ret<0)
            printf("error code %d\n",ret);
          else
            printf("wrdata:0x%x\n", wrdata);      

         }
         else
         {
          printf("Please add parameter again,r is read,w is write and add the value,too\n");
         }

close:
         close(fd);
exit:
         return 0;

}